package com.yj.suncity;

import android.app.LocalActivityManager;
import android.app.TabActivity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;
import cn.jpush.android.api.JPushInterface;

import com.yj.suncity.activity.CollectActivity;
import com.yj.suncity.activity.PublishActivity;
import com.yj.suncity.activity.SettingActivity;
import com.yj.suncity.activity.ShouYeActivity;
import com.yj.suncity.activity.UserActivity;
import com.yj.suncity.user.VersionUpdate;
import com.yj.suncity.utis.DialogUtils;
import com.yj.suncity.utis.DialogUtils.ReshActivity;

@SuppressWarnings({ "deprecation", "unused" })
public class MainActivity extends TabActivity implements OnClickListener {

	private TabHost mTabHost;
	private Intent mIntent1;
	private Intent mIntent2;
	private Intent mIntent3;
	private Intent mIntent4;
	private Intent mIntent5;
	public static final String TAB_ONE = "0";
	public static final String TAB_TWO = "1";
	public static final String TAB_THREE = "2";
	public static final String TAB_FOUR = "3";
	public static final String TAB_FIVE = "4";
	private Context context;

	private int currIndex = 0;// 当前编号
	private int zero = 0;// 动画图片偏移量
	private int one;// 单个水平动画位移
	private int two;
	private int three;
	private int four;

	// private View mView_mark;

	private ImageView imageView1, imageView2, imageView3, imageView4,
			imageView5;
	private TextView mTxt_1, mTxt_2, mTxt_3, mTxt_4, mTxt_5;
	String tagStr = "";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);
		VersionUpdate();
		int mWidth = getResources().getDisplayMetrics().widthPixels;
		one = mWidth / 5;// 设置水平动画平移大小
		two = 2 * one;
		three = 3 * one;
		four = 4 * one;
		context = this;
		mTabHost = getTabHost();
		findViews();
		addTabs();
		int tagStr = getIntent().getIntExtra("stateFlag", 0);
		showView(tagStr);

	}
	private void VersionUpdate()
	  {
	    VersionUpdate localVersionUpdate = new VersionUpdate(MainActivity.this);
	    localVersionUpdate.Version1(localVersionUpdate.getPresentVersion());
	  }
	private void findViews() {

		// mView_mark = findViewById(R.id.lay_rela_mark);
		// //RelativeLayout layout=(RelativeLayout)
		// findViewById(R.id.lay_rela_mark);
		// mView_mark.setLayoutParams(new RelativeLayout.LayoutParams(one,
		// Utils.dipTopx(context, 3)));

		findViewById(R.id.layout1).setOnClickListener(this);
		findViewById(R.id.layout2).setOnClickListener(this);
		findViewById(R.id.layout3).setOnClickListener(this);
		findViewById(R.id.layout4).setOnClickListener(this);
		findViewById(R.id.layout5).setOnClickListener(this);

		imageView1 = (ImageView) findViewById(R.id.imageview1);
		imageView2 = (ImageView) findViewById(R.id.imageview2);
		imageView3 = (ImageView) findViewById(R.id.imageview3);
		imageView4 = (ImageView) findViewById(R.id.imageview4);
		imageView5 = (ImageView) findViewById(R.id.imageview5);

		mTxt_1 = (TextView) findViewById(R.id.txt_1);
		mTxt_2 = (TextView) findViewById(R.id.txt_2);
		mTxt_3 = (TextView) findViewById(R.id.txt_3);
		mTxt_4 = (TextView) findViewById(R.id.txt_4);
		mTxt_5 = (TextView) findViewById(R.id.txt_5);

	}

	private void addTabs() {
		LocalActivityManager localActivityManager = getLocalActivityManager();
		mTabHost.setup(localActivityManager);

		mIntent1 = new Intent(this, ShouYeActivity.class);
		mIntent2 = new Intent(this, CollectActivity.class);
		mIntent3 = new Intent(this, PublishActivity.class);
		mIntent4 = new Intent(this, UserActivity.class);
		mIntent5 = new Intent(this, SettingActivity.class);

		mTabHost.addTab(mTabHost.newTabSpec(TAB_ONE).setIndicator(TAB_ONE)
				.setContent(mIntent1));
		mTabHost.addTab(mTabHost.newTabSpec(TAB_TWO).setIndicator(TAB_TWO)
				.setContent(mIntent2));
		mTabHost.addTab(mTabHost.newTabSpec(TAB_THREE).setIndicator(TAB_THREE)
				.setContent(mIntent3));
		mTabHost.addTab(mTabHost.newTabSpec(TAB_FOUR).setIndicator(TAB_FOUR)
				.setContent(mIntent4));
		mTabHost.addTab(mTabHost.newTabSpec(TAB_FIVE).setIndicator(TAB_FIVE)
				.setContent(mIntent5));
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.layout1:
			tagStr = TAB_ONE;
			break;
		case R.id.layout2:
			tagStr = TAB_TWO;
			break;
		case R.id.layout3:
			tagStr = TAB_THREE;
			break;
		case R.id.layout4:
			tagStr = TAB_FOUR;
			break;
		case R.id.layout5:
			tagStr = TAB_FIVE;
			break;
		default:
			break;
		}
		showView(Integer.parseInt(tagStr));

	}

	private void showView(int tagStr) {
		if (tagStr == currIndex) {
			return;
		}
		// Animation animation = null;
		switch (tagStr) {
		case 0:
			imageView1.setImageResource(R.drawable.shouye_press);
			mTxt_1.setTextColor(getResources().getColor(R.color.orange));
			if (currIndex == 1) {
				// animation = new TranslateAnimation(one, 0, 0, 0);
				imageView2.setImageResource(R.drawable.collect_normal);
				mTxt_2.setTextColor(getResources().getColor(R.color.txt_gray));
			} else if (currIndex == 2) {
				// animation = new TranslateAnimation(two, 0, 0, 0);
				imageView3.setImageResource(R.drawable.publish_normal);
				mTxt_3.setTextColor(getResources().getColor(R.color.txt_gray));
			} else if (currIndex == 3) {
				// animation = new TranslateAnimation(three, 0, 0, 0);
				imageView4.setImageResource(R.drawable.center_normal);
				mTxt_4.setTextColor(getResources().getColor(R.color.txt_gray));
			} else if (currIndex == 4) {
				// animation = new TranslateAnimation(four, 0, 0, 0);
				imageView5.setImageResource(R.drawable.setting_normal);
				mTxt_5.setTextColor(getResources().getColor(R.color.txt_gray));
			}

			break;
		case 1:
			imageView2.setImageResource(R.drawable.collect_press);
			mTxt_2.setTextColor(getResources().getColor(R.color.orange));
			if (currIndex == 0) {
				// animation = new TranslateAnimation(zero, one, 0, 0);
				imageView1.setImageResource(R.drawable.shouye_normal);
				mTxt_1.setTextColor(getResources().getColor(R.color.txt_gray));
			} else if (currIndex == 2) {
				// animation = new TranslateAnimation(two,one, 0, 0);
				imageView3.setImageResource(R.drawable.publish_normal);
				mTxt_3.setTextColor(getResources().getColor(R.color.txt_gray));
			} else if (currIndex == 3) {
				// animation = new TranslateAnimation(three, one, 0, 0);
				imageView4.setImageResource(R.drawable.center_normal);
				mTxt_4.setTextColor(getResources().getColor(R.color.txt_gray));
			} else if (currIndex == 4) {
				// animation = new TranslateAnimation(four, one, 0, 0);
				imageView5.setImageResource(R.drawable.setting_normal);
				mTxt_5.setTextColor(getResources().getColor(R.color.txt_gray));
			}
			break;
		case 2:
			imageView3.setImageResource(R.drawable.publish_press);
			mTxt_3.setTextColor(getResources().getColor(R.color.orange));
			if (currIndex == 0) {
				// animation = new TranslateAnimation(zero, two, 0, 0);
				imageView1.setImageResource(R.drawable.shouye_normal);
				mTxt_1.setTextColor(getResources().getColor(R.color.txt_gray));
			} else if (currIndex == 1) {
				// animation = new TranslateAnimation(one,two, 0, 0);
				imageView2.setImageResource(R.drawable.collect_normal);
				mTxt_2.setTextColor(getResources().getColor(R.color.txt_gray));
			} else if (currIndex == 3) {
				// animation = new TranslateAnimation(three, two, 0, 0);
				imageView4.setImageResource(R.drawable.center_normal);
				mTxt_4.setTextColor(getResources().getColor(R.color.txt_gray));
			} else if (currIndex == 4) {
				// animation = new TranslateAnimation(four, two, 0, 0);
				imageView5.setImageResource(R.drawable.setting_normal);
				mTxt_5.setTextColor(getResources().getColor(R.color.txt_gray));
			}
			break;
		case 3:
			imageView4.setImageResource(R.drawable.center_press);
			mTxt_4.setTextColor(getResources().getColor(R.color.orange));
			if (currIndex == 0) {
				// animation = new TranslateAnimation(zero, three, 0, 0);
				imageView1.setImageResource(R.drawable.shouye_normal);
				mTxt_1.setTextColor(getResources().getColor(R.color.txt_gray));
			} else if (currIndex == 1) {
				// animation = new TranslateAnimation(one,three, 0, 0);
				imageView2.setImageResource(R.drawable.collect_normal);
				mTxt_2.setTextColor(getResources().getColor(R.color.txt_gray));
			} else if (currIndex == 2) {
				// animation = new TranslateAnimation(two, three, 0, 0);
				imageView3.setImageResource(R.drawable.publish_normal);
				mTxt_3.setTextColor(getResources().getColor(R.color.txt_gray));
			} else if (currIndex == 4) {
				// animation = new TranslateAnimation(four, three, 0, 0);
				imageView5.setImageResource(R.drawable.setting_normal);
				mTxt_5.setTextColor(getResources().getColor(R.color.txt_gray));
			}
			break;
		case 4:
			imageView5.setImageResource(R.drawable.setting_press);
			mTxt_5.setTextColor(getResources().getColor(R.color.orange));
			if (currIndex == 0) {
				// animation = new TranslateAnimation(zero, four, 0, 0);
				imageView1.setImageResource(R.drawable.shouye_normal);
				mTxt_1.setTextColor(getResources().getColor(R.color.txt_gray));
			} else if (currIndex == 1) {
				// animation = new TranslateAnimation(one,four, 0, 0);
				imageView2.setImageResource(R.drawable.collect_normal);
				mTxt_2.setTextColor(getResources().getColor(R.color.txt_gray));
			} else if (currIndex == 2) {
				// animation = new TranslateAnimation(two, four, 0, 0);
				imageView3.setImageResource(R.drawable.publish_normal);
				mTxt_3.setTextColor(getResources().getColor(R.color.txt_gray));
			} else if (currIndex == 3) {
				// animation = new TranslateAnimation(three, four, 0, 0);
				imageView4.setImageResource(R.drawable.center_normal);
				mTxt_4.setTextColor(getResources().getColor(R.color.txt_gray));
			}
			break;
		}
		currIndex = tagStr;
		// animation.setFillAfter(true);// True:图片停在动画结束位置
		// animation.setDuration(150);
		// mView_mark.startAnimation(animation);
		mTabHost.setCurrentTabByTag(String.valueOf(tagStr));
	}

	@Override
	public boolean dispatchKeyEvent(KeyEvent event) {
		switch (event.getKeyCode()) {
		case KeyEvent.KEYCODE_BACK:
			if (event.getAction() == KeyEvent.ACTION_DOWN) {
				DialogUtils dialogUtil = DialogUtils.getInstance();
				dialogUtil.AlertDialogUtil(context, "确定退出应用吗？",
						new ReshActivity() {
							@Override
							public void reshActivity() {
								MainActivity.this.finish();
							}
						});
				return true;
			}
		default:
			return super.dispatchKeyEvent(event);
		}
	}
	@Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(getApplicationContext());
    }
    @Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		JPushInterface.onPause(getApplicationContext());
	}
}
